'use strict';
/**
 * Created by FreezeSoul on 2015/9/25.
 */
define([
        'angular',
        'app/layout/data-proxy-factory',
        'app/layout/layout-manager',
        'app/modules/player/define',
        'css!libs/golden-layout/src/css/goldenlayout-light-theme.css'
    ],
    function (angular, ProxyFactory, LayoutManager) {
        angular.module('ad.player')
            .directive('adPlayerDashboardDirective', ['$rootScope', '$state', 'adPlayerService', function ($rootScope, $state, service) {
                return {
                    templateUrl: 'app/modules/player/views/dashboard.tpl.html',
                    restrict: 'E',
                    replace: true,
                    scope: {
                        pageid: '='
                    },
                    link: function ($scope, $elm, $attrs, $ctrl) {
                        $scope.$watch('pageid', function (newValue, oldValue) {
                            if (newValue && newValue != -1) {
                                if ($scope.$parent.layoutPlugin)
                                    $scope.$parent.layoutPlugin.dispose();
                                var proxy = new ProxyFactory(service.loadDataSet).create();
                                var layout = new LayoutManager($elm[0], $scope, proxy, service.loadChart, function (pageId) {
                                    return service.loadUserPage(pageId);
                                });
                                layout.init(newValue, [], false);
                                $scope.$parent.layoutPlugin = layout;
                            }
                        }, true);

                        var resize = function () {
                            if ($scope.$parent.layoutPlugin) {
                                $scope.$parent.layoutPlugin.resize();
                            }
                        };

                        $(window).on("resize", resize);
                        $('#dashboard-content').bind('transitionend oTransitionEnd webkitTransitionEnd', resize);
                        $scope.$on('$destroy', function () {
                            $(window).off("resize");
                            $('#dashboard-content').unbind('transitionend oTransitionEnd webkitTransitionEnd', resize);
                            if ($scope.$parent.layoutPlugin) {
                                $scope.$parent.layoutPlugin.dispose();
                            }
                        });

                        $scope.$on('ad.appWeb.layout.pageLoaded', function (event, arg, arg2) {
                            if ($scope.$parent.indexPageId != arg.page_id) {
                                $scope.$parent.currentPageId = arg.page_id;
                                if ($scope.$parent.pivotPageItems.length > 0) {
                                    var lastItem = $scope.$parent.pivotPageItems[$scope.$parent.pivotPageItems.length - 1];
                                    if (lastItem.page_id == arg.page_id) {
                                        lastItem.page_param = arg2;
                                        return;
                                    }
                                }
                                $scope.$parent.pivotPageItems.push({
                                    page_id: arg.page_id,
                                    page_name: arg.page_name,
                                    page_param: arg2
                                });
                            }
                        });
                    }
                }
            }]);

    });
